﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class UIManager : MonoBehaviour {
    
    private static List<BaseUI> m_UILists = new List<BaseUI>(); 
    private const string m_path = "UIPrefabs/";
    public static GameObject m_UIParent;
    private static void UIParent()
    {
        m_UIParent = GameObject.Find("Canvas");
    }
    //加载UIPanel
	public static T EnterUI<T>()where T:BaseUI
    {
        if (m_UIParent == null)        {
            UIParent();
        }
        Debug.Log(m_path + typeof(T).ToString());
        GameObject obj = Resources.Load<GameObject>(m_path+typeof(T).ToString());
        GameObject ui = GameObject.Instantiate(obj);
        ui.transform.SetParent(m_UIParent.transform);
        ui.transform.localPosition = Vector3.zero;
        ui.transform.localRotation = Quaternion.identity;
        ui.transform.localScale = Vector3.one;

        RectTransform rect = ui.GetComponent<RectTransform>();
        rect.offsetMin = Vector2.zero;
        rect.offsetMax = Vector2.zero;
        ui.AddComponent<T>();
        ui.SetActive(true);
        T t = ui.GetComponent<T>();
        m_UILists.Add(t);
        return t;
    }
    public static void ExitUI(BaseUI baseUI)
    {
        m_UILists.Remove(baseUI);
        baseUI.ExitUI();
    }
    public static void ExitAllUI()
    {
        while(m_UILists.Count>0)
        {
            ExitUI(m_UILists[0]);
        }
        m_UILists = null;
    }
    
    //
}
